<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2022/9/24
  Time: 9:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>学生展示</title>
    <!-- 注意：项目正式环境请勿引用该地址 -->
    <link href="static\layui-v2.7.6\layui\css\layui.css" rel="stylesheet">
</head>
<body>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit" >编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="addStu">添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getData">获取当前页数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">是否全选</button>
    </div>
</script>
<form class="layui-form" action=""  id="saveUserForm" style="display: none"  lay-filter="updateUserFormFilter">
    <input type="hidden" name="uid" >

    <div class="layui-form-item">
        <label class="layui-form-label">学生名字</label>
        <div class="layui-input-block">
            <input type="text" name="uname" required  lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-inline">
            <input type="password" name="upassword" lay-verify="pass" placeholder="请输入密码" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">请填写6到12位密码</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">性别</label>
        <div class="layui-input-block">
            <input type="radio" name="usex" value="男" title="男" checked="">
            <input type="radio" name="usex" value="女" title="女">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formSaveUser">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<%--------------------------------------------------搜索框-----------------------------------------------------------%>
<form class="layui-form" action="" id="">
    <div class="demoTable">
        <span style="margin: 20px"></span> 搜索ID：
        <div class="layui-inline">
            <input class="layui-input" name="id" id="demoReload" autocomplete="off">
        </div>
        <button class="layui-btn" data-type="reload" lay-event="butid">搜索</button>
        <span style="margin: 20px"></span>搜索用户名：
        <div class="layui-inline">
            <input class="layui-input" name="username" id="demousername" autocomplete="off">
        </div>
        <button class="layui-btn" data-type="reload">搜索</button>
    </div>
</form>

<table class="layui-hide" id="test" lay-filter = "test"></table>
<!-- 注意：项目正式环境请勿引用该地址 -->
<script src="static\layui-v2.7.6\layui\layui.js"></script>
<script>
    layui.use(['table','layer','form','jquery'], function() {
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        var $ = layui.jquery;
        table.render({
            // element  元素
            elem: '#test'
            , page: true
            //,limit:10
            //,limits:[10,20,30,40]
            , toolbar: '#toolbarDemo'
            , url: 'query?method=queryAll' //  表格了里面所需要的数据就是一个list里面有很多的map、对象
            , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                // field 后面跟的内容  必须跟后台传过来的数据里面的map 的key保持一致
                , {field: 'id', title: 'ID', sort: true}
                , {field: 'username', title: '用户名'} //width 支持：数字、百分比和不填写。你还可以通过 minWidth 参数局部定义当前单元格的最小宽度，layui 2.2.1 新增
                , {field: 'sex', title: '性别', sort: true}
                , {
                    field: 'imgpath', title: '头像', sort: true, templet: function (d) {
                        return '<div><img width="100px" height="100px" src=' + d.imgpath + '></div>';
                    }
                }
                , {
                    field: 'status', title: '状态', sort: true, templet: function (d) {
                        if (d.status == 1) {
                            return '<input type="checkbox" checked value="' + d.id + '" lay-skin="switch" lay-filter="switchStatus" lay-text="启用|禁用">';
                        } else {
                            return '<input type="checkbox" value="' + d.id + '" lay-skin="switch" lay-filter="switchStatus" lay-text="启用|禁用">';
                        }
                    }
                }
                , {fixed: 'right', title: '操作', width: 125, minWidth: 125, toolbar: '#barDemo'}
            ]]
        });
        // 工具栏事件
        table.on('toolbar(test)', function (obj) {
            var id = obj.config.id;
            var checkStatus = table.checkStatus(id);
            var othis = lay(this);
            switch (obj.event) {
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(layui.util.escape(JSON.stringify(data)));
                    break;
                case 'getData':
                    var getData = table.getData(id);
                    console.log(getData);
                    layer.alert(layui.util.escape(JSON.stringify(getData)));
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选' : '未全选')
                    break;
                case 'addStu':
                    layer.open({
                        type: 1,
                        area: ['500px', '400px'],
                        offset: '100px',
                        content: $("#saveUserForm")
                    });
                    break;
            }
            ;
        });
        //触发单元格工具事件
        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            //console.log(obj)
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    $.ajax({
                        url: "shouAllServlet?method=del",
                        type: "post",
                        dataType: "JSON",
                        data: {id: data.id},//表单里所有数据
                        success: function (ret) {
                            if (ret.data == true) {
                                //关闭所有弹框
                                layer.closeAll();
                                //刷新页面
                                table.reload('test');
                            }
                        }
                    })
                });
            } else if (obj.event === 'edit') {
                layer.open({
                    type: 1,
                    area: ['500px', '400px'],
                    offset: '100px',
                    content: $("#saveUserForm")
                });
                form.val('updateUserFormFilter', data);
            }else if (obj.event === 'butid') {
                $('.demoTable .layui-btn').on('click', function () {
                    var id = $("#demoReload").val();
                    alert(id);
                    $.ajax({
                        type: "post",
                        url: "query?method=queryById",
                        dataType: "JSON",
                        data: {id: "data.id"},//表单里所有数据
                        success: function (res) {
                            alert(res.data);
                            if (ret.data == true) {
                                //关闭所有弹框
                                layer.closeAll();
                                //刷新页面
                                table.reload('test');
                            }

                        }
                    })
                });
            }
        });

        //提交表单
        // Layui提交表单 监听提交  data代表的是form表单里面的所有的内容
        form.on('submit(formSaveUser)', function (data) {
            $.ajax({
                url: "shouAllServlet?method=add",
                type: "post",
                dataType: "JSON",
                data: data.field,//表单里所有数据
                success: function (ret) {
                    if (ret.data == true) {
                        //关闭所有弹框
                        layer.closeAll();
                        //刷新页面
                        table.reload('test');
                    }
                }
            })
            return false;
        });

        /**
         * 根据id查询
         */
       /* table.on('tool(test)', function (obj) {
            var data = obj.data;
            //console.log(obj)
            if (obj.event === 'butid') {
                $('.demoTable .layui-btn').on('click', function () {
                    var id = $("#demoReload").val();
                    alert(id);
                    $.ajax({
                        type: "post",
                        url: "query?method=queryById",
                        dataType: "JSON",
                        data: {id: "data.id"},//表单里所有数据
                        success: function (res) {
                            alert(res.data);
                            if (ret.data == true) {
                                //关闭所有弹框
                                layer.closeAll();
                                //刷新页面
                                table.reload('test');
                            }

                        }
                    })
                });
            }
        });*/
    });
</script>

</body>
</html>
